﻿@model Video
@{
    ViewBag.Title = Model.Title;
    bool purchased = ContextManager.CurrentUser != null ? ContextManager.CurrentUser.Interop.OwnsVideo(Model) : false;
    var postUrl = Url.Action("VideoEvent", "Stats");
}
<script type="text/javascript">
    $(window).load(function ()
    {
        var record = function (status)
        {
            @* Note: this needs the json2.js library for older browsers that don't support native JSON parsing *@
            var d = JSON.stringify({ name: status, id: @Model.Id });
            $.ajax(
            {
                type: 'POST', dataType: 'json', url: '@postUrl', contentType: 'application/json', data: d,
                success: function() { },
                error: function() { }
            });
        }
        var recordedPlay = false;
        var recordedEnd = false;
        var host = $('#silverlightControlHost object')[0];
        if (host)
        {
            var mediaElement = host.content.findName('mediaPlayer');
            if (mediaElement && typeof mediaElement.AddEventListener == 'function')
            {
                mediaElement.AddEventListener('CurrentStateChanged', function (sender, args)
                {
                    if (sender.CurrentState == 'Playing' && !recordedPlay)
                    {
                        recordedPlay = true;
                        record('Started');
                    }
                });
                mediaElement.AddEventListener('MediaEnded', function ()
                {
                    if (!recordedEnd)
                    {
                        recordedEnd = true;
                        record('Ended');
                    }
                });
            }
        }
    });
</script>
<div class="mainDetails">
    @*
        We can show the video player even if the user doesn't own the video, we'll just omit the source.
        Even if someone requests the URL for the video source directly, we will still check if the user owns the video
        (this of course, doesn't stop a user from saving a video, once he or she has purchased it).
    *@
    <div id="silverlightControlHost">
	    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="@Model.ScaledWidth" height="@Model.ScaledHeight">
		    <param name="source" value="@Url.Content("~/ClientBin/VideoPlayer.xap")"/>
		    <param name="background" value="white" />
            @if (purchased)
            {
            <param name="initParams" value="m=V/@(Model.Id)" />
            }
            <param name="minruntimeversion" value="2.0.31005.0" />
		    <a href="http://go.microsoft.com/fwlink/?LinkId=124807" style="text-decoration: none;">
 			    <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
		    </a>
	    </object>
    </div>
    @if (purchased)
    {
        <text><h3>Feedback</h3></text>
        //show feedback if the user has purchased this video, and they haven't already given feedback
        if (!Model.Feedbacks.Any(f => f.UserInteropId == ContextManager.CurrentUser.Interop.Id))
        {
            Html.RenderPartial("VideoFeedback", new Feedback { Product = Model });
        }
        else
        {
            <p>Your feedback has been received, thank you.</p>
        }
    }
</div>
<div class="rightDetails">
    <h4>Description</h4>
    @Html.Raw(Model.Description)
    @if (!purchased)
    {
        <h4>Purchase Video</h4>
        <p>@Model.PriceString() @Html.ActionLink("Buy Now", "AddVideo", "Cart", new { id = Model.Id }, new { @class = "reg" })</p>
    }
    @Html.Partial("ListOfRelatedProducts", Model)
</div>